package com.demo.reggie.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.reggie.common.R;
import com.demo.reggie.entity.Employee;
import com.demo.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1.将页面传来的密码进行md5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        // 2.根据页面传来的用户名在数据库进行查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 创建查询条件
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        // 调用service接口进行查询
        Employee emp = employeeService.getOne(queryWrapper);

        // 3.如果没有查到数据，就返回登录失败的信息
        if (emp == null) {
            return R.error("未查询到登录用户，请重新输入");
        }

        // 4.进行密码比对，如果错误，就返回登录失败
        if(!password.equals(emp.getPassword())) {
            return R.error("用户名或密码错误，请重新输入");
        }

        // 5.查看员工状态，如果禁用，就返回对应信息
        if(emp.getStatus() == 0) {
            return R.error("该用户为禁用状态，请联系管理员");
        }

        // 6.登录成功，将员工id存入session中，并返回登录成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     * @param request 浏览器请求对象
     * @return R 消息实体
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("成功");
    }

    /**
     * 新增员工
     * @param request 浏览器请求对象
     * @param employee 员工实体
     * @return R 消息实体
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        // 设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增成功");
    }

    @PostMapping("/page")
    public R<Page<Employee>> page(@RequestBody JSONObject jsonObject) {
        // 获取分页参数
        int page = jsonObject.getInteger("pageNum");
        int pageSize = jsonObject.getInteger("pageSize");

        // 将其它的参数转为实体类
        Employee employee = jsonObject.toJavaObject(Employee.class);

        // 创建分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        // 创建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.like(employee.getName() != null, Employee::getName, employee.getName());
        queryWrapper.eq(employee.getStatus() != null, Employee::getStatus, employee.getStatus());
        queryWrapper.like(employee.getUsername() != null, Employee::getUsername, employee.getUsername());
        queryWrapper.like(employee.getPhone() != null, Employee::getPhone, employee.getPhone());

        // 添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee 传参
     * @return 消息实体
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        // 将修改人员更新为当前登录的人员
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        // 将修改时间更新为当前时间
        employee.setUpdateTime(LocalDateTime.now());
        // 调用 employeeService 中的 updateById 方法传入更新后的数据，修改数据库
        employeeService.updateById(employee);
        return R.success("操作成功");
    }
}
